package com.reeves_muiscplayer;

import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ListActivity {
    // 播放对象
    private MediaPlayer mediaPlayer;
    // 播放列表
    private List<String> musicList = new ArrayList<String>();
    // 当前播放歌曲的索引
    private int currentListItem = 0;
    // 音乐的路径
    private static final String MUSIC_PATH = getSDPath()+"/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaPlayer = new MediaPlayer();
        findView();
        musicList();
        listener();
    }

    // 初始化界面控件
    public void findView() {
        ViewHolder.last = (Button) this.findViewById(R.id.btnLast);
        ViewHolder.pause = (Button) this.findViewById(R.id.btnPause);
        ViewHolder.start = (Button) this.findViewById(R.id.btnStart);
        ViewHolder.stop = (Button) this.findViewById(R.id.btnStop);
        ViewHolder.next = (Button) this.findViewById(R.id.btnNext);
    }

    // 绑定数据
    void musicList() {
        File home = new File(MUSIC_PATH);
        if (home.listFiles(new MusicFilter()).length > 0) {
            for (File file : home.listFiles(new MusicFilter())) {
                musicList.add(file.getName());
            }
            ArrayAdapter<String> musicAdapter = new ArrayAdapter<String>
                    (MainActivity.this, R.layout.music_item, musicList);
            setListAdapter(musicAdapter);
        }
    }

    //监听事件
    void listener() {
        //停止
        ViewHolder.stop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.reset();
                }
            }
        });
        //开始
        ViewHolder.start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                playMusic(MUSIC_PATH + musicList.get(currentListItem));
            }
        });
        //下一首
        ViewHolder.next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                nextMusic();
            }
        });
        //暂停
        ViewHolder.pause.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                } else {
                    mediaPlayer.start();
                }
            }
        });
        //上一首
        ViewHolder.last.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                lastMusic();
            }
        });

    }

    //播放音乐
    void playMusic(String path) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    nextMusic();
                }
            });
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    //下一首
    void nextMusic() {
        if (++currentListItem >= musicList.size()) {
            currentListItem = 0;
        } else {
            playMusic(MUSIC_PATH + musicList.get(currentListItem));
        }
    }

    //上一首
    void lastMusic() {
        if (currentListItem != 0) {
            if (--currentListItem >= 0) {
                currentListItem = musicList.size();
            } else {
                playMusic(MUSIC_PATH + musicList.get(currentListItem));
            }
        } else {
            playMusic(MUSIC_PATH + musicList.get(currentListItem));
        }
    }

    //当用户返回时结束音乐并释放音乐对象
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            mediaPlayer.stop();
            mediaPlayer.release();
            this.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    //当选择列表项时播放音乐
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        currentListItem = position;
        playMusic(MUSIC_PATH + musicList.get(currentListItem));
    }

    /**
     * 工具方法
     */
    public static String getSDPath() {
//        File sdDir = null;
//        boolean sdCardExist = Environment.getExternalStorageState()
//                .equals(Android.os.Environment.MEDIA_MOUNTED);   //判断sd卡是否存在
//        if(sdCardExist)
//        {
//            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
//        }r
//        return sdDir.toString();
        System.out.println("sdcard path: --->>"+Environment.getExternalStorageDirectory().getPath());
        return Environment.getExternalStorageDirectory().getPath();

    }


}
